/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: ProjZoneManageServiceImpl.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年1月15日 下午3:43:01
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.basedata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.material.c2c.basedata.dao.ProjZoneManageDao;
import com.material.c2c.basedata.entity.DLXX_XZDY;
import com.material.c2c.basedata.entity.DLXX_XZDY_EXP;
import com.material.c2c.basedata.model.ZoneManageDTO;
import com.material.c2c.core.UUIDGenerator;

/**
 * 
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Service
public class ProjZoneManageServiceImpl implements ProjZoneManageService{
  @Autowired
  private ProjZoneManageDao ProjZoneManageDao;

  @Autowired
  private CacheManager cacheManager;

  @Override
  @Cacheable(cacheNames = "zoneCache", key = "'subZoneList:' + #parentId")
  public List<ZoneManageDTO> findSubZoneListByParrentId(String parentId) {
    // -1是设定的值，表示取根级的行政地域信息。便于进行缓存的key值生成
    if ("-1".equals(parentId)) {
      return ProjZoneManageDao.findAllRootZoneList();
    } else {
      return ProjZoneManageDao.findSubZoneListByParrentId(parentId);
    }
  }

  @Override
  @CacheEvict(cacheNames = "zoneCache", key = "'subZoneList:' + #parentZoneId")
  public void add(DLXX_XZDY entity, String parentZoneId) {
    ProjZoneManageDao.add(entity);

    // 新增的一条地域信息，同时需要将该信息增加到行政地域扩展表中，并且sjid和zjid都是自己，便于查询功能的使用
    DLXX_XZDY_EXP currentZoneExpEntity = new DLXX_XZDY_EXP();
    currentZoneExpEntity.setId(UUIDGenerator.generate().toString());
    currentZoneExpEntity.setSjid(entity.getId());
    currentZoneExpEntity.setSjjb(entity.getJb());
    currentZoneExpEntity.setSjbm(entity.getBm());
    currentZoneExpEntity.setZjid(entity.getId());
    currentZoneExpEntity.setZjbm(entity.getBm());
    currentZoneExpEntity.setZjjb(entity.getJb());
    ProjZoneManageDao.addZoneExpInfo(currentZoneExpEntity);

    List<Object[]> parentZoneList = ProjZoneManageDao.findAllParentZoneListById(entity.getSjid());
    if (null != parentZoneList && !parentZoneList.isEmpty()) {
      for (Object[] objects : parentZoneList) {
        // objects为3的位置返回的是该条记录的sjid信息，如果上级地域id不为null，那就表示没有到达根级地域，那就需要维护行政地域的扩展表。
        if (null != objects[3]) {
          DLXX_XZDY_EXP expEntity = new DLXX_XZDY_EXP();
          expEntity.setId(UUIDGenerator.generate().toString());
          expEntity.setSjid(null != objects[0] ? objects[0].toString() : "");
          expEntity.setSjjb(null != objects[1] ? Integer.parseInt(objects[1].toString()) : 0);
          expEntity.setSjbm(null != objects[2] ? objects[2].toString() : "");
          expEntity.setZjid(entity.getId());
          expEntity.setZjbm(entity.getBm());
          expEntity.setZjjb(entity.getJb());
          ProjZoneManageDao.addZoneExpInfo(expEntity);
        }
      }
    }
  }

  @Override
  @Caching(evict = { @CacheEvict(cacheNames = "zoneCache", key = "'subZoneList:' + #parentZoneId"),
      @CacheEvict(cacheNames = "zoneCache", key = "#entity.id") })
  public void update(DLXX_XZDY entity, String parentZoneId) {
    ProjZoneManageDao.update(entity);
    ProjZoneManageDao.updateZoneExpDataSJBM(entity.getId(), entity.getBm());
    ProjZoneManageDao.updateZoneExpDataZJBM(entity.getId(), entity.getBm());
  }

  @Override
  @Cacheable(cacheNames = "zoneCache", key = "#id")
  public DLXX_XZDY getEntityById(String id) {
    return ProjZoneManageDao.getOne(id);
  }

  @Override
  public boolean validateZoneExistsByJbAndBm(int jb, String bm, String id) {
    List<DLXX_XZDY> entityList = ProjZoneManageDao.findByJbAndBmAndIdNot(jb, bm, id);
    // 返回的结果集不为null并且不为空，表示查询到记录，那就返回true。
    if (null != entityList && !entityList.isEmpty()) {
      return true;
    }
    return false;
  }

  @Override
  public List<String> getIdStrByZoneIdAndChildrenZoneId(String id) {
    List<String> resultList = ProjZoneManageDao.findZoneIdsByZoneIdAndChildrenZoneId(id);
    // 如果返回一个查询为空的结果集，那就放入一个空串，防止后续的包含in的sql语句报错
    if (null != resultList && resultList.isEmpty()) {
      resultList.add("");
    }
    return resultList;
  }

  @Override
  public boolean isExistsContracteeData(List<String> ids) {
    List<String> resultList = ProjZoneManageDao.findContracteeIdsByZoneIdAndChildrenZoneId(ids);
    // 返回的结果集不为null并且不为空，表示查询到记录，那就返回true。
    if (null != resultList && !resultList.isEmpty()) {
      return true;
    }
    return false;
  }

  @Override
  public void deleteSpecifyZoneAndSubZone(List<String> ids, String parentZoneId) {
    ProjZoneManageDao.deleteZoneByZoneIdAndChildrenZoneId(ids);
    ProjZoneManageDao.deleteExpZoneByZoneIdAndChildrenZoneId(ids);
    // 在删除地域信息的时候因为要迭代删除其下的所有子地域信息，所以手动进行关联缓存的清理，无法使用@CacheEvict的方式来进行完成
    Cache cache = cacheManager.getCache("zoneCache");
    cache.evict("subZoneList:" + parentZoneId);
    if (null != ids && !ids.isEmpty()) {
      for (String id : ids) {
        cache.evict("subZoneList:" + id);
        cache.evict(id);
      }
    }
  }

  @Override
  public DLXX_XZDY findByBm(String bm) {
    return ProjZoneManageDao.findByBm(bm);
  }

  @Override
  public String getSjjc(String qxdm) {
    DLXX_XZDY sjdy = findByBm(qxdm.substring(0, 2));
    String sjjc = sjdy.getJc();
    if (!StringUtils.hasText(sjjc)) {
      sjjc = sjdy.getMc();
    }
    return sjjc;
  }
}
